package ibm.cognos.imt

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 1/13/14
 * Time: 5:33 PM
 * To change this template use File | Settings | File Templates.
 */

class Filter {

    def dataItem
    def dataType
    def values = new ArrayList<>()
    def range
    def compare
    def inValues
    def owner


    def dataType() {
        if (dataItem.dataType==1) {
            return "integer"
        }
        if (dataItem.dataType==2) {
            return "decimal"
        }
        if (dataItem.dataType==3) {
            return "string"
        } else{
            "unknown"
        }
    }

    Filter (exp, report){
        owner = report
        def dataItemName = exp.substring(exp.indexOf("[")+1, exp.indexOf("]"))
        for (item in owner.dataItemList) {
            if (dataItemName.equals(item.name)) {
                dataItem = item
            }
        }
        dataType = dataItem.dataType
        if (exp.contains(">")) {
            compare = "greaterThan"
            def matcher = (exp=~ /\[(\S+)\]\s+>\s+(\d+[.\d]+)/)
            if (matcher.matches()) {
                for (item in owner.dataItemList) {
                    if(item.name == matcher[0][1]){
                        dataItem = item
                        values<<matcher[0][2]
                    }
                }
            }
        }
        if (exp.contains("between")) {
            range = "yes"
            def matcher = (exp =~ /\[(\S+)\]\s+between\s+([.\d]+)\sand\s+([.\d]+)/)
            if (matcher.matches()) {
                for (item in owner.dataItemList) {
                    if (item.name == matcher[0][1]) {
                        dataItem = item
                        values<<matcher[0][2]
                        values<<matcher[0][3]
                    }
                }
            }
        }
        if (exp.contains("includes")) {
            inValues = "yes"
            def results = parseIncludedValues(exp)
                for (item in owner.dataItemList) {
                    if (item.name == dataItemName) {
                        dataItem = item
                        for (value in results) {
                            values<<value
                        }
                    }
                }
        }

    }

    def parseIncludedValues(exp) {
        def result = exp.substring(exp.indexOf("(") + 1, exp.indexOf(")"))
        def values = result.tokenize(",")
        def x = new ArrayList<>()
        for (value in values) {
            x.add(value.trim().replace("'",""))
        }
        x
    }

    def generateSpec(builder) {
        builder.detailFilter(postAutoAggregation:"true", use:"required"){
            filterDefinition {
                if (compare != null) {
                    filterCompare(refDataItem:"${dataItem.name}", dataType:"${dataType()}", operator:"$compare") {
                        filterValue(values[0])
                    }
                }
                if(range!=null){
                    filterRange(refDataItem: "${dataItem.name}", dataType:"${dataType()}") {
                        filterFrom("${values[0]}")
                        filterTo("${values[1]}")
                    }
                }
                if (inValues != null) {
                    filterInValues(refDataItem: "${dataItem.name}", dataType:"${dataType()}") {
                        filterValues {
                            for (value in values) {
                                filterValue("$value")
                            }
                        }
                    }
                }

            }
        }
    }



}
